package com.video.main_video.webConfig;

import com.video.main_video.common.response.ZouResponseBodyHandleReturnValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private RequestHeaderFilter requestHeaderFilter;

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {

        returnValueHandlers.add((HandlerMethodReturnValueHandler) new ZouResponseBodyHandleReturnValue());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").
                exposedHeaders("Authorization"
                        , "access-control-allow-headers",
                        "Cookie"
                );
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(requestHeaderFilter).excludePathPatterns("/login").excludePathPatterns("/register")
                .excludePathPatterns("/api/login").excludePathPatterns("api/login")
                .excludePathPatterns("/api/register")
//                .excludePathPatterns("/api/upload")
//                .excludePathPatterns("/upload")
                .excludePathPatterns("/getVideo")
//                .excludePathPatterns("/api/download")
                .excludePathPatterns("api/loginOut");
    }
}
